/**
 * Licensed by the Bunie Developmentgroup.
 */
package Protocol;

/**
 * @author Wouter Class is used to request to log into the server, useally send
 * from client to server
 */
public class InlogRequestPacket extends BasicPacket {

    private String username;
    private String password;
    private int typeOfClient;

    /**
     * Constructor of the inlogrequestpacket
     *
     * @param version Version of his inlogrequestpacket
     */
    public InlogRequestPacket(int version) {
        super(version);
        this.type = PacketTypeKeeper.INLOGREQUESTPACKET;
    }

    /**
     * Methode for encrypting strings/passwords
     *
     * @param password The string to be encrypted
     * @return Encrypted version of the input
     */
    private static String encrypt(String password) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(password.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }

    /**
     * Methode to set the username of the inlogrequestpacket
     *
     * @param username Value to which the username must be set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Methode to return the username of this inlogrequestpacket
     *
     * @return Username of this inlogrequest
     */
    public String getUsername() {
        return this.username;
    }

    /**
     * Methode to set the password of this inlogrequestpacket
     *
     * @param password Value to set the password of this inlogrequestpacket to
     */
    public void setPassword(String password) {
        this.password = encrypt(password);
    }

    /**
     * Methode to return encryped password
     *
     * @return encrypted password
     */
    public String getPassword() {
        return this.password;
    }

    /**
     * Methode to get the type of client that send the inlogrequestpacket
     * @return Type of client
     */
    public int getTypeOfClient() {
        return this.typeOfClient;
    }

    /**
     * methode to set the type of client that send this inlogrequestpacket
     *
     * @param typeOfClient Type of client that send this inlogrequestpacket
     */
    public void setTypeOfClient(int typeOfClient) {
        this.typeOfClient = typeOfClient;
    }

    @Override
    public int getType() {
        return this.type;
    }

    @Override
    public String toString() {
        return super.toString()
                + " username:"+ username + " "
                + " type of client: "+ typeOfClient;// PacketTypeKeeper.translate(typeOfClient);
                
    }
}